#include <stdio.h>
#include <string.h>
#include "los_task.h"
#include "cmsis_os2.h"
#include "shell.h"

// 全局变量定义
UINT32 g_highTaskID, g_lowTaskID;     // 任务ID
UINT32 g_highPrintCount = 0;         // 高级别任务打印计数
UINT32 g_lowPrintCount = 0;          // 低级别任务打印计数
UINT32 g_taskControl = 1;            // 任务控制标志（1=允许打印，0=禁止打印）

// 学号和姓名（请替换为你的实际信息）
const char *g_studentID = "2023095033";
const char *g_studentName = "刘金南";

// 高级别任务（打印学号）
UINT32 HighPriorityTask(void)
{
    while (1) {
        // 检查任务控制标志
        if (g_taskControl == 0) {
            printf("High Task: Print Permission Denied!\n");
            break;
        }
        
        // 打印学号
        printf("High Priority Task - Student ID: %s\n", g_studentID);
        g_highPrintCount++;
        
        // 打印2次后等待低级别任务处理
        if (g_highPrintCount >= 2) {
            printf("High Task: Wait for low task to control.\n");
            break;
        }
        
        // 任务延时，让出CPU
        LOS_TaskDelay(100);
    }
    
    // 任务挂起，等待低级别任务恢复
    while (1) {
        LOS_TaskDelay(100);  // 防止任务空转
    }
    
    return LOS_OK;
}

// 低级别任务（打印姓名）
UINT32 LowPriorityTask(void)
{
    while (1) {
        // 检查高级别任务是否已完成打印
        if (g_highPrintCount >= 2 && g_taskControl == 1) {
            // 禁止高级别任务打印
            g_taskControl = 0;
            printf("Low Task: High task print permission disabled!\n");
            
            // 挂起高级别任务
            if (g_highTaskID != 0) {
                UINT32 ret = LOS_TaskSuspend(g_highTaskID);
                if (ret == LOS_OK) {
                    printf("Low Task: High task suspended successfully.\n");
                } else {
                    printf("Low Task: Suspend high task failed!\n");
                }
            }
        }
        
        // 打印姓名
        printf("Low Priority Task - Student Name: %s\n", g_studentName);
        g_lowPrintCount++;
        
        // 打印3次后结束任务
        if (g_lowPrintCount >= 3) {
            printf("Low Task: Print 3 times completed, closing tasks.\n");
            break;
        }
        
        // 任务延时，让出CPU
        LOS_TaskDelay(100);
    }
    
    // 关闭所有任务
    if (g_highTaskID != 0) {
        UINT32 ret = LOS_TaskDelete(g_highTaskID);
        if (ret == LOS_OK) {
            printf("Low Task: High task deleted successfully.\n");
        } else {
            printf("Low Task: Delete high task failed!\n");
        }
    }
    
    if (g_lowTaskID != 0) {
        UINT32 ret = LOS_TaskDelete(g_lowTaskID);
        if (ret == LOS_OK) {
            printf("Low Task: Low task deleted successfully.\n");
        } else {
            printf("Low Task: Delete low task failed!\n");
        }
    }
    
    return LOS_OK;
}

// 任务创建函数
UINT32 CreateTaskExample(void)
{
    UINT32 ret;
    TSK_INIT_PARAM_S taskParam;
    
    // 锁任务调度
    LOS_TaskLock();
    
    // 创建高级别任务
    memset(&taskParam, 0, sizeof(TSK_INIT_PARAM_S));
    taskParam.pfnTaskEntry = (TSK_ENTRY_FUNC)HighPriorityTask;
    taskParam.usTaskPrio = 20;
    taskParam.uwStackSize = 4096;
    taskParam.pcName = "HighTask";
    
    ret = LOS_TaskCreate(&g_highTaskID, &taskParam);
    if (ret != LOS_OK) {
        LOS_TaskUnlock();
        printf("Create high priority task failed!\n");
        return LOS_NOK;
    }
    printf("High priority task created successfully.\n");
    
    // 创建低级别任务
    memset(&taskParam, 0, sizeof(TSK_INIT_PARAM_S));
    taskParam.pfnTaskEntry = (TSK_ENTRY_FUNC)LowPriorityTask;
    taskParam.usTaskPrio = 27;
    taskParam.uwStackSize = 4096;
    taskParam.pcName = "LowTask";
    
    ret = LOS_TaskCreate(&g_lowTaskID, &taskParam);
    if (ret != LOS_OK) {
        LOS_TaskUnlock();
        printf("Create low priority task failed!\n");
        return LOS_NOK;
    }
    printf("Low priority task created successfully.\n");
    
    // 解锁任务调度
    LOS_TaskUnlock();
    
    return LOS_OK;
}

// // 命令回调函数
// UINT32 MyTaskCmd(UINT32 argc, const CHAR **argv)
// {
//     printf("Executing task example...\n");
//     return CreateTaskExample();
// }

// 命令注册函数
void RegisterTaskCommand(void)
{
    osCmdReg(CMD_TYPE_EX, "MyTask", 0, CreateTaskExample);
    printf("Task command registered successfully.\n");
}